package com.yfp.device.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.device.domain.*;
import com.yfp.device.mapper.ClientHealthLogMapper;
import com.yfp.device.service.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *
 * @description 针对表【client_health_log(客户健康餐饮记录)】的数据库操作Service实现
 * @createDate 2025-02-11 16:25:49
 */
@Service
public class ClientHealthLogServiceImpl extends ServiceImpl<ClientHealthLogMapper, ClientHealthLog> implements ClientHealthLogService {

    @Resource
    private ClientService clientService;
    @Resource
    private ComboService comboService;
    @Resource
    private OrderListService orderListService;
    @Resource
    private OrderComboService orderComboService;


    @Override
    public void orderFinishSave(String orderDetailId) {
        OrderList orderList = orderListService.getById(orderDetailId);
        OrderCombo orderCombo = orderComboService.selectByid(orderList.getOrderSn());
        Client client = clientService.getById(orderCombo.getUserId());
        if (Objects.isNull(client) ||Objects.isNull(client.getHeight()) || Objects.isNull(client.getWeight())) {
            return;
        }

        Combo combo = comboService.getById(orderList.getComboId());

        ClientHealthLog clientHealthLog = new ClientHealthLog();
        clientHealthLog.setOrderListId(orderDetailId);
        clientHealthLog.setWeight(combo.getWeight());
        clientHealthLog.setKcal(combo.getKcal());
        clientHealthLog.setProtein(combo.getProtein());
        clientHealthLog.setFat(combo.getFat());
        clientHealthLog.setCarbohydrate(combo.getCarbohydrate());
        clientHealthLog.setStatus(1);
        clientHealthLog.setCreateTime(DateUtil.date());
        clientHealthLog.setClientId(orderCombo.getUserId());
        baseMapper.insert(clientHealthLog);
    }
}




